<?php
namespace Home\Controller;

use Think\Controller;

class AddressesController extends CommonController
{
  protected $this_table = "addresses";

  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  //创建信息
  public function create()
  {
    $payload = file_get_contents('php://input');
    $post_data = json_decode($payload, true);

    $model = M($this->this_table);
    $now_time = gmtime();

    $post_data['addr'] = trim($post_data['addr']);
    $post_data['receiver_name'] = trim($post_data['receiver_name']);
    $post_data['receiver_mobile'] = trim($post_data['receiver_mobile']);
    unset($post_data['updated_at']);
    $post_data['user_id'] = $this->user_id;

    //验证合法性
    if (!$post_data['user_id']) {
      return_data("error", $this->this_table.".user_id.blank");
    }

    if($post_data['is_wechat_addr']){
      $post_data['province'] = $post_data['province_name'];
      $post_data['city'] = $post_data['city_name'];
      $post_data['area'] = $post_data['area_name'];

      $post_data['province_code'] = $post_data['city_code'] = $post_data['area_code'] = 1;
    }else{
      if (!$post_data['province_code']) {
        return_data("error", $this->this_table.".province_code.blank");
      } else {
        $province = M("areaes")->where("status=1 and code='".$post_data['province_code']."'")->getField("name");
        if (!$province) {
          return_data("error", $this->this_table.".province.error");
        }

        $post_data['province']=$province;
      }

      if (!$post_data['city_code']) {
        return_data("error", "create.city_code.blank");
      } else {
        $city = M("areaes")->where("status=2 and code='".$post_data['city_code']."'")->getField("name");
        if (!$city) {
          return_data("error", $this->this_table.".city.error");
        }

        $post_data['city'] = $city;
      }

      if (!$post_data['area_code']) {
        return_data("error", "create.area_code.blank");
      } else {
        $area = M("areaes")->where("status=3 and code='".$post_data['area_code']."'")->getField("name");
        if (!$area) {
          return_data("error", $this->this_table.".area.error");
        }

        $post_data['area'] = $area;
      }
    }

    if (!$post_data['addr']) {
      return_data("error", $this->this_table.".addr.blank");
    }
    if (!$post_data['receiver_name']) {
      return_data("error", $this->this_table.".receiver_name.blank");
    }
    if (!$post_data['receiver_mobile']) {
      return_data("error", $this->this_table.".receiver_mobile.blank");
    }
    //手机验证----11位
    // if (strlen($post_data['receiver_mobile']) != 11) {
    //   return_data("error", $this->this_table.".receiver_mobile.eleven.number");
    // }
    // if (!preg_match("/^1[0-9]{10}$/", $post_data['receiver_mobile'])) {
    //   return_data("error", $this->this_table.".receiver_mobile.error");
    // }

    $post_data['created_at'] = $now_time;
    $post_data['updated_at'] = $now_time;
    $model->create($post_data);
    $id = $model->add();

    if (empty($id)) {
      return_data("error", "create.".$this->this_table.".error");//blank
    } else {
      return_data("success", "create.".$this->this_table.".success", $id);
    }
  }

  //获取信息
  public function show()
  {
    $model = M($this->this_table);
    $id = trim(I('id'));


    if (!$id) {
      return_data("error", $this->this_table.".id.blank");
    }

    $where['id'] = $id;
    $where['user_id'] = $this->user_id;
    $field = "*";
    $info = $model->field($field)->where($where)->find();

    if (empty($info)) {
      return_data("error", "show.".$this->this_table.".empty");//blank
    } else {
      return_data("success", "show.".$this->this_table.".success", $info);
    }
  }

  public function areas()
  {
    $model = M("areaes");
    $code = trim(I('code'));

    if (!$code) {
      $status = 1;
    } else {
      if ($code % 10000 > 0) {
        $code = $code / 100;
        $status = 3;
      } else {
        $code = $code / 10000;
        $status = 2;
      }
    }

    $where = "code like '$code%' and status = $status";

    $field = "code, name";
    $list = $model->field($field)->where($where)->select();

    if ($list[0]['name'] == '市辖区' and $status == 3) {
      array_shift($list);
    }

    if ($list) {
      return_data("success", "show.areaes.success", $list);
    } else {
      return_data("error", "show.areaes.empty");
    }
  }

  //获取列表
  public function index()
  {
    $where['user_id'] = $this->user_id;
    $list = M($this->this_table)->where($where)->order('id desc')->select();

    if ($list) {
      return_data('success', "index.".$this->this_table.".success", $list);
    } else {
      return_data('error', "index.".$this->this_table.".empty");
    }
  }

  //修改
  public function update()
  {
    $payload = file_get_contents('php://input');
    $post_data = json_decode($payload, true);

    $model = M($this->this_table);
    $now_time = $this->current_time;

    $id= trim($post_data['id']);
    $post_data['addr'] = trim($post_data['addr']);
    $post_data['receiver_name'] = trim($post_data['receiver_name']);
    $post_data['receiver_mobile'] = trim($post_data['receiver_mobile']);
    unset($post_data['created_at']);
    unset($post_data['user_id']);
    unset($post_data['id']);

    //验证合法性
    if (!$post_data['province_code']) {
      return_data("error", $this->this_table.".province_code.blank");
    } else {
      $province = M("areaes")->where("status=1 and code='".$post_data['province_code']."'")->getField("name");
      if (!$province) {
        return_data("error", $this->this_table.".province.error");
      }

      $post_data['province'] = $province;
    }

    if (!$post_data['city_code']) {
      return_data("error", $this->this_table.".city_code.blank");
    } else {
      $city = M("areaes")->where("status=2 and code='".$post_data['city_code']."'")->getField("name");
      if (!$city) {
        return_data("error", $this->this_table.".city.error");
      }

      $post_data['city'] = $city;
    }

    if (!$post_data['area_code']) {
      return_data("error", $this->this_table.".area_code.blank");
    } else {
      $area = M("areaes")->where("status=3 and code='".$post_data['area_code']."'")->getField("name");
      if (!$area) {
        return_data("error", $this->this_table.".area.error");
      }

      $post_data['area'] = $area;
    }

    if (!$post_data['addr']) {
      return_data("error", $this->this_table.".addr.blank");
    }
    if (!$post_data['receiver_name']) {
      return_data("error", $this->this_table.".receiver_name.blank");
    }
    if (!$post_data['receiver_mobile']) {
      return_data("error", $this->this_table.".receiver_mobile.blank");
    }
    //手机验证----11位
    if (strlen($post_data['receiver_mobile']) != 11) {
      return_data("error", $this->this_table.".receiver_mobile.eleven.number");
    }
    if (!preg_match("/^1[0-9]{10}$/", $post_data['receiver_mobile'])) {
      return_data("error", $this->this_table.".receiver_mobile.error");
    }

    $model->create($post_data);
    $post_data["updated_at"] = $now_time;
    $where['id'] = $id;
    $where['user_id'] = $this->user_id;
    $result = $model->where($where)->save($post_data);
    if ($result) {
      return_data("success", "update.".$this->this_table.".success");
    } else {
      return_data("error", "update.".$this->this_table.".error");
    }
  }

  //删除
  public function delete()
  {
    $model = M($this->this_table);
    $now_time = $this->current_time;

    $id = trim(I("id"));
    $where['id'] = $id;
    $where['user_id'] = $this->user_id;
    $result = $model->where($where)->delete();

    if ($result) {
      return_data("success", "delete.".$this->this_table.".success");
    } else {
      return_data("error", "delete.".$this->this_table.".error");
    }
  }
}
